package httpHandle

import (
	"github.com/gin-gonic/gin"
	"github.com/wzh200x/webServer/constants"
	"github.com/wzh200x/webServer/httpHandle/service"
	"github.com/wzh200x/webServer/middleware/auth"
	"github.com/wzh200x/webServer/middleware/response"
)

// 使用service包中定义的请求和响应结构
type GetCenterRequest service.GetCenterRequest
type UpdateCenterRequest service.UpdateCenterRequest
type CenterResponse service.CenterResponse

// HandleGetCenter 处理获取区服信息请求
func HandleGetCenter(c *gin.Context) {
	success, err := response.GetResponseFuncs(c)

	// 从上下文中获取用户信息
	userID, username := auth.GetCurrentUser(c)

	// 调用service层获取区服信息
	center, errCode, errMsg := service.GetCenter(userID, username)
	if errCode > 0 {
		err(errCode, errMsg)
		return
	}

	// 返回区服信息
	success(center)
}

// HandleUpdateCenter 处理更新区服信息请求
func HandleUpdateCenter(c *gin.Context) {
	success, err := response.GetResponseFuncs(c)

	// 从上下文中获取用户信息
	userID, username := auth.GetCurrentUser(c)

	// 解析请求参数
	var req UpdateCenterRequest
	if bindErr := c.ShouldBind(&req); bindErr != nil {
		err(constants.ErrUnknown, "无效的请求参数")
		return
	}

	// 转换为service层的请求结构
	serviceReq := service.UpdateCenterRequest{
		Data: req.Data,
	}

	// 调用service层更新区服信息
	center, errCode, errMsg := service.UpdateCenter(userID, username, serviceReq)
	if errCode > 0 {
		err(errCode, errMsg)
		return
	}

	// 返回更新后的区服信息
	success(center)
}
